import { Toast } from 'antd-mobile';
import { ACTION_TYPES } from 'utils/constant';
import { Get, Post } from 'utils/handler';


export const getFriendList = id => dispatch => {
    dispatch({
        type: ACTION_TYPES.FRIEND_LIST_REQUEST
    })
    Get('/friend/list', {
        data: {
            id
        }
    }).then(res => {
        if (res.reasonCode === 200) {
            dispatch({
                type: ACTION_TYPES.FRIEND_LIST_SUCCESS,
                users: res.users
            })
        } else {
            dispatch({
                type: ACTION_TYPES.FRIEND_LIST_FAIL
            })
        }
    }).catch(e => {
        dispatch({
            type: ACTION_TYPES.FRIEND_LIST_FAIL
        })
    })
}

export const friendAdd = data => dispatch => {
    const { from, to } = data;
    dispatch({
        type: ACTION_TYPES.FRIEND_ADD_REQUEST
    })

    return Post('/friend/add', { from, to }).then(res => {
        if (res.reasonCode === 200) {
            dispatch({
                type: ACTION_TYPES.FRIEND_ADD_SUCCESS
            })
            return true;
        } else {
            dispatch({
                type: ACTION_TYPES.FRIEND_ADD_FAIL
            })
            Toast.fail(res.message);
            return false;
        }
    }).catch(e => {
        dispatch({
            type: ACTION_TYPES.FRIEND_ADD_FAIL
        })
        return false;
    })
}